//
//  AllQJViewController.m
//  svqj
//
//  Created by xftt on 14-6-23.
//
//

#import "QJViewController.h"
#import "QJView.h"
#import "SortMenuView.h"
#import "NewWebViewController.h"
#import "MainViewController.h"

@interface QJViewController ()
{
    NSMutableArray *_QJData;
    NSMutableArray *allData;
    UIScrollView *_scroll;
    int _type;
    BOOL isEdit;
}

@end

@implementation QJViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    _scroll = [[UIScrollView alloc]initWithFrame:_contentView.bounds];
    [_contentView addSubview:_scroll];
    [_editButton addTarget:self action:@selector(doEdit) forControlEvents:UIControlEventTouchUpInside];
    [self reloadList];
}

-(void)viewWillAppear:(BOOL)animated
{
    if (isEdit) {
        [self doEdit];
    }
    [self reloadList];
}

- (void)reloadList
{
//    [_editButton setHidden:YES];
    switch (_type) {
        case 1:
            [_editButton setHidden:NO];
            [self loadNilSortData];
            break;
        case 2:
            [_editButton setTitle:@"设置" forState:UIControlStateNormal];
            [self loadTempData];
            break;
        case 3:
            [_editButton setHidden:NO];
            [self loadSortData:_sortInfo];
            break;
    }
}

- (id)initWithType:(int)type
{
    self = [super initWithNibName:nil bundle:nil];
    if (self) {
        _type = type;
    }
    return self;
}

- (void)loadNilSortData
{
    if (self.navigationController !=nil) {
        [_backView setHidden:NO];
        UITapGestureRecognizer * reg = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doBack)];
        [_backView addGestureRecognizer:reg];
        [_lbTitle setText:@"未分类全景"];
    }
    _QJData = [NSMutableArray array];
    NSUserDefaults *_Data = [NSUserDefaults standardUserDefaults];
    allData = [NSMutableArray arrayWithArray:[_Data objectForKey:@"QJData"]];
    for (int i=0; i<[allData count]; i++) {
        NSDictionary *dic = [allData objectAtIndex:i];
        if ([[dic objectForKey:@"sortid"] length] == 0) {
            [_QJData addObject:dic];
        }
    }
    [self loadQJList];
}

- (void)loadTempData
{
    _QJData = [NSMutableArray array];
    NSUserDefaults *_Data = [NSUserDefaults standardUserDefaults];
    _QJData = [_Data objectForKey:@"QJTemp"];
    [self loadQJList];
}

- (void)loadSortData:(NSDictionary*)sortinfo
{
    [_backView setHidden:NO];
    UITapGestureRecognizer * reg = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doBack)];
    [_backView addGestureRecognizer:reg];
    
    _QJData = [NSMutableArray array];
    NSUserDefaults *_Data = [NSUserDefaults standardUserDefaults];
    allData = [NSMutableArray arrayWithArray:[_Data objectForKey:@"QJData"]];
    [_lbTitle setText:[sortinfo objectForKey:@"sortname"]];
    
    NSString *sortid = [sortinfo objectForKey:@"sortid"];
    for (int i=0; i<[allData count]; i++) {
        NSDictionary *dic = [allData objectAtIndex:i];
        if ([[dic objectForKey:@"sortid"] isEqualToString:sortid]) {
            [_QJData addObject:dic];
        }
    }
    [self loadQJList];
}

- (void)loadQJList
{
    [_scroll removeSubviews];
    for (int i=0; i<[_QJData count]; i++) {
        QuanJing *qj = [[QuanJing alloc]initWithDictionary:[_QJData objectAtIndex:i]];
        [_scroll addSubview:[self getViewWithQuanJing:qj AndIndex:i]];
    }
    CGSize size = CGSizeMake(_scroll.frame.size.width, (([_QJData count]-1)/3+1)*300+42<698?699:(([_QJData count]-1)/3+1)*300+42);
    [_scroll setContentSize:size];
}

- (UIView *)getViewWithQuanJing:(QuanJing*)qj AndIndex:(NSInteger)index
{
    NSInteger line = index/3;
    NSInteger row = index-line*3;
    
    QJView *_qjview = [[QJView alloc] initWithFrame:CGRectMake(42+262*row, 50+300*line, 236, 250)];
    [_qjview setQuanjing:qj];
    [_qjview setViewEdit:isEdit];
    
    UITapGestureRecognizer * reg = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doClick:)];
    [_qjview.imgvQJ addGestureRecognizer:reg];
    _qjview.imgvQJ.tag = index;
    _qjview.FController = self;
    return _qjview;
}

- (void)doBack
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)doEdit
{
    if(_type==2)
    {
        [(AppDelegate*)[[UIApplication sharedApplication] delegate] firstLoad];
    }
    else
    {
        isEdit = !isEdit;
        for(UIView*view in _scroll.subviews)
        {
            if ([view isKindOfClass:[QJView class]]) {
                [(QJView*)view setViewEdit:isEdit];
            }
        }
        
        if (isEdit) {
            [_editButton setTitle:@"取消" forState:UIControlStateNormal];
        }
        else
        {
            for (id view in _contentView.subviews) {
                if ([view isKindOfClass:[SortMenuView class]]) {
                    [view removeFromSuperview];
                }
            }
            [_editButton setTitle:@"编辑" forState:UIControlStateNormal];
        }
    }
}

- (void)doClick:(UITapGestureRecognizer*)sender
{
    QuanJing *qj = [[QuanJing alloc] initWithDictionary:[_QJData objectAtIndex:sender.view.tag]];
    
    NSUserDefaults *_Data = [NSUserDefaults standardUserDefaults];
    NSMutableArray *_QJTemp = [NSMutableArray arrayWithArray:[_Data objectForKey:@"QJTemp"]];
    
    BOOL isNew = YES;
    for (NSDictionary *qjdictionary in _QJTemp) {
        if([[qjdictionary objectForKey:@"sid"] isEqualToString:qj.sid])
        {
            [_QJTemp removeObject:qjdictionary];
            [_QJTemp insertObject:qjdictionary atIndex:0];
            isNew = NO;
            break;
        }
    }
    
    if (isNew) {
        [_QJTemp insertObject:qj.dic atIndex:0];
        if ([_QJTemp count] > 6) {
            [_QJTemp removeLastObject];
        }
    }
    [_Data setObject:_QJTemp forKey:@"QJTemp"];
    [_Data synchronize];
    
    MainViewController *main = (MainViewController*)self.tabFrameController;
    if(main==nil)
    {
        main = (MainViewController*)self.navigationController.tabFrameController;
    }
    [main showQJ:qj];
}

- (void)saveSort:(NSDictionary*)qj from:(NSDictionary*)qjing
{
    NSInteger  row = [_QJData indexOfObject:qjing];
    [allData removeObject:qjing];
    [allData insertObject:qj atIndex:row];
    
    NSUserDefaults *_Data = [NSUserDefaults standardUserDefaults];
    [_Data setValue:allData forKey:@"QJData"];
    [_Data synchronize];

    [self reloadList];
}

-(void)selectSortMenu:(NSMutableDictionary*)quanjing
{
    NSUserDefaults *_Data = [NSUserDefaults standardUserDefaults];
    NSArray* QJSort = [_Data objectForKey:@"QJSort"];
    SortMenuView *sortMenu = [[SortMenuView alloc]initWithFrame:_contentView.bounds];
    [sortMenu setFController:self];
    [sortMenu setQuanjing:quanjing];
    [sortMenu setQJSort:QJSort];
    [_contentView addSubview:sortMenu];
}

@end
